from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
import pandas as pd
import time
import re
import json



class Paypal(object):


    # path = r"C:\paypal\chromedriver.exe"
    driver = webdriver.Chrome()
    
    
    #登陆
    def Login(self):
        # 调用 user.tx 里的账号密码
        alist = []
        f2 = open("./user.txt","r")
        lines = f2.readlines()
        for line3 in lines:
            alist.append(line3)
        username = alist[0]
        password = alist[1]

        #selenium 登陆页面
        self.driver.get('https://www.paypal.com/c2/signin')
        time.sleep(3)

        #登陆账号
        self.driver.find_element_by_id('email').send_keys(username)
        time.sleep(5)
        
        #登陆密码
        self.driver.find_element_by_id('password').send_keys(password)
        time.sleep(5)

        #点击登陆
        self.driver.find_element_by_id('btnLogin').click()
        

        locator = (By.CLASS_NAME, "css-htcxmu")
        time.sleep(40)
        
        try:
            WebDriverWait(self.driver, 20, 1).until(
                EC.presence_of_element_located(locator))
            time.sleep(2)
            print('logined success1')
        except:
            self.driver.delete_all_cookies()
            print('login error')
            time.sleep(5)
            py.Login()


    def Receive(self):

        print('登陆成功') 
        #点击 交易记录                  
        self.driver.find_element_by_xpath("/html/body/div/div[1]/div/div[1]/div[2]/div/div[1]/ul/li[4]/a").click()
        time.sleep(3)

        #点击 空白处
        self.driver.find_element_by_xpath("/html/body/div[3]/div[1]/div[1]/div/span[1]/input").click()
        time.sleep(3)
        #点击 全部   
        self.driver.find_element_by_xpath("/html/body/div[3]/div[1]/div[1]/div/div/div/div/button").click()
        time.sleep(3)
        #点击 交易号                         /html/body/div[3]/div[1]/div[1]/div/div/div/div/ul/li[3]/a        
        self.driver.find_element_by_xpath("/html/body/div[3]/div[1]/div[1]/div/div/div/div/ul/li[3]/a").click()
        time.sleep(3)

        #从excel表格里找到交易号
        #df = pd.read_excel((r'C:\Users\Administrator\Desktop\download\paypal.xls'),encoding='UTF-8',) 
        # df = pd.read_excel((r'C:\Users\Administrator\Desktop\download\abcd.xls'),encoding='UTF-8',) 
        
        # list = []
        # #取到每一个交易号
        # for row in df.iterrows():
        #     jycode = row[1]['交易号']
        #     ydcode = row[1]['运单号']
        #     wulfs = row[1]['物流方式']
        #     list.append(jycode)
        
        # putcode   = list[0]
        #输入交易号         
        self.driver.find_element_by_xpath("/html/body/div[3]/div[1]/div[1]/div/span[1]/input").send_keys('90P35411585800516')
        time.sleep(3)
        #点击搜索
        self.driver.find_element_by_id("search").click()
        time.sleep(5)
        # 获取搜索结果框里的列表                                 
        table_rows = self.driver.find_elements_by_css_selector('#searchTransactions table tbody tr')
        time.sleep(5)

        if table_rows:
            
            # 第一行搜索结果
            js = "document.getElementById('searchTransactions').querySelector('tbody').querySelector('tr').querySelectorAll('td')[6].querySelector('a').click()"
            self.driver.execute_script(js)
            time.sleep(3)
            print('has get one')

            #找到编辑跟踪信息
            bjmessage = self.driver.find_element_by_xpath("/html/body/div/div[2]/div/div[2]/div[2]/div/div[2]/section[2]/div/div/div[2]/ul/li[1]/a")
            message = bjmessage.text
            print(message)

            #如果有 点击
            if message:
                #点击编辑跟踪信息
                self.driver.find_element_by_xpath("/html/body/div/div[2]/div/div[2]/div[2]/div/div[2]/section[2]/div/div/div[2]/ul/li[1]/a").click()
                
                self.driver.find_element_by_id("trackingNumer").clear()

                #输入 跟踪号
                self.driver.find_element_by_id("trackingNumer").send_keys(ydcode)

                #选择其它
                self.driver.find_element_by_id("carrier[last()]").click()

                #输入 运送方式
                self.driver.find_element_by_id("otherCarrierName").send_keys(wulfs)

                #点击提交
                self.driver.find_element_by_xpath("//*[@id=addTrackingRoot]/div/form/div[3]/div[1]/span").click()

                list.pop(0)

                for i in range(1,df.shape[0]+1):
                    py.Receive()

            else:
                #点击 添加跟踪信息
                self.driver.find_element_by_xpath("/html/body/div/div[2]/div/div[2]/div[2]/div/div[1]/section/div/div/div[2]/ul/li[2]/a").click()


                #输入 跟踪号
                self.driver.find_element_by_id("trackingNumer").send_keys(ydcode)

                #选择其它
                self.driver.find_element_by_id("carrier[last()]").click()

                #输入 运送方式
                self.driver.find_element_by_id("otherCarrierName").send_keys(wulfs)

                #点击提交
                self.driver.find_element_by_xpath("//*[@id=addTrackingRoot]/div/form/div[3]/div[1]/span").click()

                list.pop(0)

                for i in range(1,df.shape[0]+1):
                    py.Receive()

        else:
            print("未找到链接")






























py = Paypal()
py.Login()
py.Receive()


input()





















